#!/bin/sh

# .........................................................................
# Pulls out dihedral angles from MD trajectory
# The number of frames is determined by the wc command
# For each entry it then changes to that directory and set of unix commands
# to remove header lines from data files, cut out the data columns and
# paste files together to give the final data file
# It also builds a file containing the frame number of the global energy
# minimum structure.

echo '**********************************************************************'
echo  'Dihedrals analysis of a spin label'
echo  'run from directory containing pdb files'
echo  'type Dihedral followed by name of dcd file'
echo '**********************************************************************'

# .........................................................................

# Assign start values to variables

probe="CMTS"
	echo $probe
crdroot="build"
	echo $crdroot
psfroot="build"
	echo $psfroot

for element in $*
do
        dcdroot=$element
echo $dcdroot

	numframes=`awk 'END{print(NR-3)}' $dcdroot.ene`
	print $numframes

		awk '/CMTS/ && /SG/ {print $8 " " $9}' build.crd > label.txt
	labelsegid1=`awk 'NR==1 {print $1}' label.txt`
	labelresid1=`awk 'NR==1 {print $2}' label.txt`
	echo label_1 $labelresid1 "  " segment $labelsegid1;

	charmm < $SL_MOD/charmm/charmm_inp/getdihe_CMTS.inp \
        numframes="$numframes" \
        labresid="$labelresid1" \
        labsegid="$labelsegid1" \
        crdroot="$crdroot" \
	psfroot="$psfroot" \
	dcdroot="$dcdroot" \
	firsttime="$first" \
	lasttime="$numframes"\
 	skiptime="$skip" >out

	awk 'NR>15 {print}' dihe.dat > $dcdroot.dihe-dat

	done
